<?php
class ProductAction extends CommonAction {

	
	private $table = "product" ;
	
	public function catalogtree(){
		$this->assign("containList",$this->getCatalogTree());	
		$this->display() ;
	}
	
	
	public function add(){
		
		//编辑产品流程
		if(!empty($_REQUEST ['id'])){
			$condition['id'] = $_REQUEST ['id'] ;
			
			$vo = $this->CommonSimplequery($condition, "viewproduct",1);			
			$this->assign( "productVo" ,$vo);
		}
		$productUnit_condition['status'] = 1 ;
		$this->assign("productUnitList",$this->CommonSimplequery($productUnit_condition,'product_unit',0));
		
		$productUnit_condition['status'] = 1 ;
		$this->assign("productColorList",$this->CommonSimplequery($productUnit_condition,'product_color',0));
		
		//dump($vo);
		//dump($this->CommonSimplequery($productUnit_condition,'product_unit',0));
		
		$this->display ();	
		
	}
	
	
	/**
	 * 修改状态 
	 * 
	 */
	public function statusHandle(){
		$model = D( $this->table );		
		$id = $_REQUEST ['id'];		
		$condition['id'] = $id ;
		$vo = $this->CommonSimplequery($condition, $this->table,1);
		$data['status'];
		if($vo['status'] == 1){
			$data['status'] = 0  ;
		}else{
			$data['status'] = 1  ;
		}
   	    $this->resultHandle($model->where($condition)->save($data),"产品状态修改成功！","产品状态修改失败！") ;

	}
	
	/**
	 * 
	 * 保存产品信息
	 */
	public function saveProduct(){
  		
		$model = D( $this->table );		
		
		$data["name"] = $_REQUEST ['name'];
		
		$data["pro_catalog_id"] = $_REQUEST ['district_id'];		
		//根据产品分类id获取到产品类型id
		$productcatalog_condition['id'] = $_REQUEST ['district_id'] ;
		
		$productcatalogVo = $this->CommonSimplequery($productcatalog_condition,'product_catalog',1);
		
		$data["pro_type_id"] = $productcatalogVo['type_id'];	
		
		$data["local_price"] = $_REQUEST ['local_price'];	
			
		$data["market_price"] = $_REQUEST ['market_price'];	
		$data["quantity"] = $_REQUEST ['quantity'];		
		$data["pro_payment_id"] = $_REQUEST ['pro_payment_id'];
		$data["shippinginstructions"] = $_REQUEST ['shippinginstructions'];
		$data["information"] = stripslashes($_REQUEST ['information']);
		$data["recommend"] = $_REQUEST ['recommend'];
		$data["location"] = $_REQUEST ['location'];
		$data["createdate"] = date('Y-m-d H:i:s',time());
		$data["statusdate"] = date('Y-m-d H:i:s',time());		
		$data['pro_unit_id'] = $_REQUEST['productUnit'];
		$data['pro_color_id'] = $_REQUEST['productColor'];
		
		//新增
		if(empty($_REQUEST ['id'])){
			$imgName = $this->_upload();			
			$data["imageurl_small"] = "s_".$imgName ;
			$data["imageurl_big"] = "m_".$imgName ;
			$data["status"] = 1 ;
			//获取产品类型		
			$save_result = $model->add($data) ;
			$this->resultHandle($save_result,"新增产品成功！","新增产品失败！");
		}
		//修改
		else{
			//如果上传了图片就要修改字段值
			if( $_FILES['image']['name'] != ''){
				
				$imgName = $this->_upload();
				$data["imageurl_small"] = "s_".$imgName ;
				$data["imageurl_big"] = "m_".$imgName ;
			}
			$data["statusdate"] = date('Y-m-d H:i:s',time());	
			$condition['id'] = $_REQUEST ['id'] ;
			$save_result = $model->where($condition)->save($data) ;
			$this->resultHandle($save_result,"修改产品成功！","修改产品失败！");
		}
	}
	
	
	
	
	
	public function upload() {
		
		
        if (!empty($_FILES)) {
            //如果有文件上传 上传附件
            
            $this->_upload();
            //$this->forward();
        }else{
        
        	return '' ;
        }
        
    }

    
	
	
	
	
	//框架首页
	private function getCatalogTree() {
		//按顺序查询产品类型				
		$model = D ( "product_type" );
		$condition['status'] = 1 ;
		$typeList = $model->where($condition)->order('createdate desc')->select();
		$this->assign("typeList",$typeList);
		//循环取出产品类型		
		$containList ;
		for ($i= 0;$i<count($typeList); $i++){
			$tempList = $typeList[$i];				
			$containList[$i] = $this->getSubFolder(0,$tempList['id'],$i) ;					
		}
		return $containList ;	
	}
	

	private function getSubFolder($path,$type,$index){
		$str = "" ;		
		$subFolderCondition['childid'] = array('NEQ','0');
		$subFolderCondition['parentid'] = $path ;	
		$subFolderCondition['type_id'] = $type ;
		$subFolderCondition['status'] = 1 ;
		$subFolderList = $this->CommonSimplequery($subFolderCondition,'product_catalog');
		for ($i= 0;$i< count($subFolderList); $i++){		
				$tempList = $subFolderList[$i];
				//dump($tempList) ;						
				if($tempList['parentid'] == 0 ){
					//$str = $str . "<ul class='tree treeFolder treeCheck expand' oncheck='treecheck'>" ;	
					$str = $str . "<ul class='tree treeFolder  expand' >" ;
				}else{
					$str = $str . "<ul>" ;
				}				
				
				//onclick="$.bringBack({id:1, districtName:'东城', cityName:'北京'})"
				
				$str = $str . "<li><a href='javascript:' onclick=" . '"' . "$.bringBack({id:'" . $tempList['id'] . "', districtName:'" . $tempList['name'] . "'}) " . '"' . " rel='".($index+1) ."'>" . $tempList['name']  . "</a>";
				//遍历子结点				
				$str = $str . $this->getSubFolder($tempList['id'],$type,$index) ;
  				$str = $str . $this->getSubDirect($tempList['id'],$type,$index) ;
				$str = $str . "</li>" ;		
				$str = $str . "</ul>" ;				
		} 		
		return $str;		
	}
	
	private function getSubDirect($path,$type){
		$str = "" ;
		$subDirectCondition['childid'] = '0';
		$subDirectCondition['parentid'] = $path ;
		$subFolderCondition['type_id'] = $type ;
		$subFolderCondition['status'] = 1 ;
		$subDirectList = $this->CommonSimplequery($subDirectCondition,'product_catalog');
		for ($i= 0;$i< count($subDirectList); $i++){
				$tempList = $subDirectList[$i];				
				$str = $str . "<ul>" ;
				$str = $str . "<li><a href='javascript:' onclick=" . '"' . "$.bringBack({id:'" . $tempList['id'] . "', districtName:'" . $tempList['name'] . "'}) " . '"' . " rel='".($index+1)."'>" . $tempList['name'] . "</a></li>" ;	
				$str = $str . "</ul>" ;
		}
		return $str;		
	}	

}
?>